package com.xbao.service.impl;

import com.xbao.core.util.MyBeanUtil;
import com.xbao.dao.EmpOrgRelMapper;
import com.xbao.dao.OrgExtraMapper;
import com.xbao.dao.OrgMapper;
import com.xbao.model.EmpOrgRel;
import com.xbao.model.EmpOrgRelExample;
import com.xbao.model.Org;
import com.xbao.model.OrgExample;
import com.xbao.model.req.OrgReq;
import com.xbao.model.vo.EmpVo;
import com.xbao.model.vo.OrgVo;
import com.xbao.service.EmpService;
import com.xbao.service.OrgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 18/8/28.
 */
@Service
public class OrgServiceImpl implements OrgService {
    @Resource
    private OrgMapper orgMapper;
    @Resource
    private EmpService empService;
    @Resource
    private EmpOrgRelMapper empRelMapper;
    @Resource
    private OrgExtraMapper orgExtraMapper;


    @Override
    public void addOrg(OrgReq orgReq) {
        orgMapper.insert(orgReq);

    }

    @Override
    public OrgVo detailOrg(Long id) {
        Org org = orgMapper.selectByPrimaryKey(id);
        OrgVo vo = MyBeanUtil.copyProperties(org, OrgVo.class);
        return vo;
    }

    @Override
    public void deleteOrg(Long id) {

        orgMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void detailOrgAppend(Long oid) {
        OrgVo vo = detailOrg(oid);
        EmpVo empVo = empService.detailEmp(vo.getOwnid());
        vo.setEmpVo(empVo);

    }

    @Override
    public List<OrgVo> queryOrgs(OrgReq req) {
        OrgExample example = new OrgExample();
        OrgExample.Criteria criteria = example.createCriteria();
        List<Org> orgs = orgMapper.selectByExample(example);
        List<OrgVo> vos = MyBeanUtil.copyList(orgs, OrgVo.class);
        return vos;
    }

    @Override
    public void modifyOrg(OrgReq orgReq) {
        OrgExample orgExample = new OrgExample();
        OrgExample.Criteria criteria = orgExample.createCriteria();
        if (orgReq.getId() != null)
            criteria.andIdEqualTo(orgReq.getId());
        orgMapper.updateByExample(orgReq, orgExample);
    }

    @Override
    public void joinOrg(EmpOrgRel orgReq) {
        //orgMapper.joinOrg(orgReq);
        empRelMapper.insert(orgReq);


    }

    @Override
    public void leaveOrg(EmpOrgRel orgReq) {
        EmpOrgRelExample example = new EmpOrgRelExample();
        EmpOrgRelExample.Criteria criteria = example.createCriteria();
        criteria.andEidEqualTo(orgReq.getEid());
        criteria.andOidEqualTo(orgReq.getOid());
        empRelMapper.deleteByExample(example);

    }

    @Override
    public List<OrgVo> myOrgs(OrgReq req) {
        EmpOrgRelExample example = new EmpOrgRelExample();
        EmpOrgRelExample.Criteria criteria = example.createCriteria();
        criteria.andEidEqualTo(req.getId());
        List<EmpOrgRel> vs = empRelMapper.selectByExample(example);
        List<OrgVo> vos = new ArrayList<>();
        for (EmpOrgRel rel : vs) {
            OrgVo vo = detailOrg(rel.getOid());
            vos.add(vo);


        }
        return vos;


    }

    public void setMainOrg(EmpOrgRel rel){
        orgExtraMapper.resetUnMainOrg(rel.getEid());
        empRelMapper.updateByPrimaryKeySelective(rel);
    }








    @Override
    public OrgVo baseOrgInfo(Long id) {
        OrgVo vo = detailOrg(id);
        Integer empcount = orgExtraMapper.getEmpCountOfOrg(id);
        Integer rcount = orgExtraMapper.getRoomCountOfOrg(id);
        Integer scount = orgExtraMapper.getSubjectCountOfOrg(id);
        vo.setEmpcount(empcount);
        vo.setRoomcount(rcount);
        vo.setSubjectcount(scount);
        return vo;
    }
}
